local skel = fk.CreateSkill {
  name = "rmt__guojie",
  tags = {Skill.Limited},
  dynamic_desc = function (self, player, lang)
    if player:getMark("rmt__guojie_limit") == 0 then
      return "rmt__guojie_nolimit"
    end
  end,
}

Fk:loadTranslationTable{
  ["rmt__guojie"] = "国劫",
  [":rmt__guojie"] = "限定技，出牌阶段，若你某个区域有牌且为全场最多，你可以结束本回合，然后执行两项或两次一项：视为使用一张未以此法使用过的伤害类牌；重置一个技能；删除武将牌上一个技能中的“限定技”标签。",

  [":rmt__guojie_nolimit"] = "出牌阶段，若你某个区域有牌且为全场最多，你可以结束本回合，然后执行两项或两次一项：视为使用一张未以此法使用过的伤害类牌；重置一个技能；删除武将牌上一个技能中的“限定技”标签。",

  ["#rmt__guojie"] = "国劫:你可以结束本回合，然后执行两项或两次一项",
  ["@@rmt__guojie"] = "国劫-限定",
  ["#rmt__guojie-choice"] = "国劫：请选择一项！（第 %arg 次，共 2 次）",
  ["rmt__guojie_use"] = "视为使用一张未以此法使用过的伤害类牌",
  ["rmt__guojie_reset"] = "重置一个技能",
  ["rmt__guojie_delete"] = "删除武将牌上一个技能中的“限定技”标签",
  ["@$rmt__guojie_names"] = "国劫",
  ["#rmt__guojie-reset"] = "国劫：选择你要重置的技能！",
  ["#rmt__guojie-delete"] = "国劫：选择一个限定技，删除其“限定技”标签！",

  ["$rmt__guojie1"] = "天下丧乱，肃宗暴崩，皆是因由尔等！",
  ["$rmt__guojie2"] = "洛内所传为真？陛下何故害臣？",
  ["$rmt__guojie3"] = "尔朱世隆无胆，朝中谁敢生此心？",
}

skel:addEffect("active", {
  mute = true,
  card_num = 0,
  target_num = 0,
  prompt = "#rmt__guojie",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return (player:usedSkillTimes(skel.name, Player.HistoryGame) == 0 or player:getMark("rmt__guojie_limit") == 0)
    and table.find({"h", "e", "j"}, function (flag)
      return #player:getCardIds(flag) > 0 and
      table.every(Fk:currentRoom().alive_players, function(p) return #p:getCardIds(flag) <= #player:getCardIds(flag) end)
    end)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    --player:broadcastSkillInvoke(skel.name)
    player.room:notifySkillInvoked(player, skel.name, player:getMark("rmt__guojie_limit") == 0 and "control" or "big")
    room:setPlayerMark(player, "rmt__guojie-turn", 1)
    room:endTurn()
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "rmt__guojie_limit", 1)
  player:setSkillUseHistory(skel.name, 0, Player.HistoryGame)
  player.room:setPlayerMark(player, "@$rmt__guojie_names", 0)
end)

skel:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, "rmt__guojie_limit", 1)
  player:setSkillUseHistory(skel.name, 0, Player.HistoryGame)
  local names = {}
  for _, name in ipairs(Fk:getAllCardNames("bt")) do
    if Fk:cloneCard(name).is_damage_card then
      table.insert(names, name)
    end
  end
  player.room:setPlayerMark(player, "@$rmt__guojie_names", names)
end)

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("rmt__guojie-turn") ~= 0 and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skillName = skel.name
    for i = 1, 2 do
      if player.dead then break end
      local all_choices = {"rmt__guojie_use", "rmt__guojie_reset", "rmt__guojie_delete"}
      local choices = {}
      local names = table.filter(player:getTableMark("@$rmt__guojie_names"), function(name)
        local card = Fk:cloneCard(name)
        card.skillName = skillName
        return player:canUse(card, {bypass_times = true}) and not player:prohibitUse(card)
      end)
      if #names > 0 then
        table.insert(choices, "rmt__guojie_use")
      end
      local skills = player:getSkillNameList()
      if #skills > 0 then
        table.insert(choices, "rmt__guojie_reset")
      end
      local limit_skills = table.filter({"rmt__jiangbing", "rmt__guojie"}, function (name)
        return player:hasSkill(name, true) and player:getMark(name .. "_limit") ~= 0
      end)
      if #limit_skills > 0 then
        table.insert(choices, "rmt__guojie_delete")
      end
      if #choices == 0 then break end
      local choice = room:askToChoice(player, { choices = choices, skill_name = skillName,
      prompt = "#rmt__guojie-choice:::"..i, all_choices = all_choices })
      player:broadcastSkillInvoke(skel.name, table.indexOf(all_choices, choice)) -- 根据选项播配音
      if choice == "rmt__guojie_use" then
        local use = room:askToUseVirtualCard(player, {
          name = names, skill_name = skillName, cancelable = false, skip = true,
        })
        if use then
          local mark = table.filter(player:getTableMark("@$rmt__guojie_names"), function(name)
            return Fk:cloneCard(name).trueName ~= use.card.trueName
          end)
          room:setPlayerMark(player, "@$rmt__guojie_names", #mark > 0 and mark or 0)
          room:useCard(use)
        end
      elseif choice == "rmt__guojie_reset" then
        choice = room:askToChoice(player, { choices = skills, skill_name = skillName, prompt = "#rmt__guojie-reset", detailed = true })
        player:setSkillUseHistory(choice, 0, Player.HistoryGame)
        local skill = Fk.skills[choice]
        local skeleton = skill:getSkeleton()
        if skeleton then
          skeleton:onLose(player, false)
          skeleton:onAcquire(player, false)
        end
      elseif choice == "rmt__guojie_delete" then
        choice = room:askToChoice(player, { choices = limit_skills, skill_name = skillName, prompt = "#rmt__guojie-delete", detailed = true })
        room:setPlayerMark(player, choice.."_limit", 0)
        local skill = Fk.skills[choice]
        local skeleton = skill:getSkeleton()
        if skeleton then
          skeleton.tags = {}
        end
        player:setSkillUseHistory(choice, 0, Player.HistoryGame)
      end
    end
  end,
})

return skel
